package com.tjtt.tdm.knowledge.dao.impl;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import org.hibernate.Query;
import org.hibernate.Session;

import com.tjtt.tdm.base.BaseHibernateDao;
import com.tjtt.tdm.base.PageResult;
import com.tjtt.tdm.base.StringUtil;
import com.tjtt.tdm.exception.DaoException;
import com.tjtt.tdm.exception.ServiceException;
import com.tjtt.tdm.knowledge.bean.KnowFileType;
import com.tjtt.tdm.knowledge.bean.KnowledgeHandOut;
import com.tjtt.tdm.knowledge.bean.KnowledgeManage;
import com.tjtt.tdm.knowledge.dao.IKnowledgeManageDao;
/**
 * 
 * @author songxiaoyan
 *
 */
public class KnowledgeManageDaoImpl extends BaseHibernateDao implements IKnowledgeManageDao{

	public PageResult getAllAfterList(String fileType,Map filterMap) throws DaoException {
		StringBuffer queryString = new StringBuffer();
		StringBuffer queryCountString = new StringBuffer();
		
		queryCountString.append("select count(*) from (select ID,FILE_NAME,FILE_SAMPLEID,FILE_DOCUMENTTYPE,FILE_DOCUMENTTYPE_NAME,FILE_ENTRYADDRESS,FILE_AUTHOR,FILE_VERSION, "+
							" FILE_PAGE,FILE_VOLUMENUMBER,FILE_CUSTOS,FILE_CLASSIFICATIONTYPE,FILE_PRICE,FILE_PUBLICATIONDATE,FILE_WRITEDATE, "+
							" FILE_INCORPORATEDATE,FILE_PUBLISHINGCOMPANY,FILE_SUBJECT,FILE_SUMMARY,FILE_REMARKS,FILE_AUDITOR,FILE_CHECKDATE, "+
							" FILE_LOOKNEWDAYS,FILE_LOOKNEWDATE,FILE_CHECKSUGGESTION,FILE_ISPUBLIC,FILE_TYPE,FILE_ADD,SETUP_USER_NAME,SETUP_TIME "+
							" from TDM_FILE_MANAGE where 1=1  ");
		queryString.append("select ID,FILE_NAME,FILE_SAMPLEID,FILE_DOCUMENTTYPE,FILE_DOCUMENTTYPE_NAME,FILE_ENTRYADDRESS,FILE_AUTHOR,FILE_VERSION, "+
							" FILE_PAGE,FILE_VOLUMENUMBER,FILE_CUSTOS,FILE_CLASSIFICATIONTYPE,FILE_PRICE,FILE_PUBLICATIONDATE,FILE_WRITEDATE, "+
							" FILE_INCORPORATEDATE,FILE_PUBLISHINGCOMPANY,FILE_SUBJECT,FILE_SUMMARY,FILE_REMARKS,FILE_AUDITOR,FILE_CHECKDATE, "+
							" FILE_LOOKNEWDAYS,FILE_LOOKNEWDATE,FILE_CHECKSUGGESTION,FILE_ISPUBLIC,FILE_TYPE,FILE_ADD,SETUP_USER_NAME,SETUP_TIME "+
							" from TDM_FILE_MANAGE where 1=1 ");
		if (filterMap != null && !filterMap.isEmpty()) {
			if(null != filterMap.get("fileType") && !"".equals(filterMap.get("fileType"))){
				queryCountString.append("and FILE_TYPE = '" +  filterMap.get("fileType") + "' ");
				queryString.append("and FILE_TYPE = '" +  filterMap.get("fileType") + "' ");
			}
			if(null != filterMap.get("fileName") && !"".equals(filterMap.get("fileName"))){
				queryCountString.append("and FILE_NAME like '%" + filterMap.get("fileName") + "%'  ");
				queryString.append("and FILE_NAME like '%" + filterMap.get("fileName") + "%'  ");
			}
			if(null != filterMap.get("fileSampleId") && !"".equals(filterMap.get("fileSampleId"))){
				queryCountString.append("and FILE_SAMPLEID like '%" + filterMap.get("fileSampleId") + "%' ");
				queryString.append("and FILE_SAMPLEID like '%" + filterMap.get("fileSampleId") + "%' ");
			}
			if(null != filterMap.get("fileDocumentType") && !"".equals(filterMap.get("fileDocumentType"))){
				String str = filterMap.get("fileDocumentType").toString();
				System.out.println(str);
//				queryString.append("and str like '%" + filterMap.get("fileDocumentType") + "%' ");
			}
			queryCountString.append(")");
		}
		return searchBySQL(queryCountString.toString(), queryString.toString(), filterMap);
	}
	
	public PageResult getAllList(Map filterMap) throws DaoException {
		StringBuffer queryString = new StringBuffer();
		StringBuffer queryCountString = new StringBuffer();
		
		queryCountString.append("select count(*) from (select ID,FILE_NAME,FILE_SAMPLEID,FILE_DOCUMENTTYPE,file_documenttype_name,FILE_ENTRYADDRESS,FILE_AUTHOR,FILE_VERSION,"+
		" FILE_PAGE,FILE_VOLUMENUMBER,FILE_CUSTOS,FILE_CLASSIFICATIONTYPE,FILE_PRICE,FILE_PUBLICATIONDATE,FILE_WRITEDATE,"+
		" FILE_INCORPORATEDATE,FILE_PUBLISHINGCOMPANY,FILE_SUBJECT,FILE_SUMMARY,FILE_REMARKS,FILE_AUDITOR,FILE_CHECKDATE,"+
		" FILE_LOOKNEWDAYS,FILE_LOOKNEWDATE,FILE_CHECKSUGGESTION,FILE_ISPUBLIC,FILE_TYPE,FILE_ADD,SETUP_USER_NAME,SETUP_TIME"+
		" from TDM_FILE_MANAGE where 1=1 ");
		queryString.append("select ID,FILE_NAME,FILE_SAMPLEID,FILE_DOCUMENTTYPE,file_documenttype_name,FILE_ENTRYADDRESS,FILE_AUTHOR,FILE_VERSION,"+
		" FILE_PAGE,FILE_VOLUMENUMBER,FILE_CUSTOS,FILE_CLASSIFICATIONTYPE,FILE_PRICE,FILE_PUBLICATIONDATE,FILE_WRITEDATE,"+
		" FILE_INCORPORATEDATE,FILE_PUBLISHINGCOMPANY,FILE_SUBJECT,FILE_SUMMARY,FILE_REMARKS,FILE_AUDITOR,FILE_CHECKDATE,"+
		" FILE_LOOKNEWDAYS,FILE_LOOKNEWDATE,FILE_CHECKSUGGESTION,FILE_ISPUBLIC,FILE_TYPE,FILE_ADD,SETUP_USER_NAME,SETUP_TIME"+
		" from TDM_FILE_MANAGE where 1=1  ");
		if (filterMap != null && !filterMap.isEmpty()) {
			if(null != filterMap.get("fileType") && !"".equals(filterMap.get("fileType"))){
				queryCountString.append("and FILE_TYPE = '" +  filterMap.get("fileType") + "' ");
				queryString.append("and FILE_TYPE = '" +  filterMap.get("fileType") + "' ");
			}
			if(null != filterMap.get("fileName") && !"".equals(filterMap.get("fileName"))){
				queryCountString.append("and FILE_NAME like '%" + filterMap.get("fileName") + "%'  ");
				queryString.append("and FILE_NAME like '%" + filterMap.get("fileName") + "%'  ");
			}
			if(null != filterMap.get("fileSampleId") && !"".equals(filterMap.get("fileSampleId"))){
				queryCountString.append("and FILE_SAMPLEID like '%" + filterMap.get("fileSampleId") + "%' ");
				queryString.append("and FILE_SAMPLEID like '%" + filterMap.get("fileSampleId") + "%' ");
			}
			if(null != filterMap.get("fileDocumentTypeName") && !"".equals(filterMap.get("fileDocumentTypeName"))){
				queryCountString.append("and file_documenttype_name like '%" + filterMap.get("fileDocumentTypeName") + "%' ");
				queryString.append("and file_documenttype_name like '%" + filterMap.get("fileDocumentTypeName") + "%' ");
			}
			String sidx = (String)filterMap.get("sidx");
			String sord = (String)filterMap.get("sord");
			queryString.append(" order by  " + sidx + " " + sord );
			queryCountString.append(") ");
		}
		return searchBySQL(queryCountString.toString(), queryString.toString(), filterMap);
	}
	
	public PageResult queryKnowledgeManageListByPatentId(String fileType,
			Map<String, String> filterMap) throws DaoException {
		StringBuffer queryString = new StringBuffer();
		StringBuffer queryCountString = new StringBuffer();
		
		queryCountString.append("select count(*) from TDM_FILE_MANAGE where 1=1 and FILE_TYPE = "+ fileType +"");
		queryString.append("select ID,FILE_NAME,FILE_SAMPLEID,FILE_DOCUMENTTYPE,FILE_DOCUMENTTYPE_NAME,FILE_ENTRYADDRESS,FILE_AUTHOR,FILE_VERSION,");
		queryString.append(" FILE_PAGE,FILE_VOLUMENUMBER,FILE_CUSTOS,FILE_CLASSIFICATIONTYPE,FILE_PRICE,FILE_PUBLICATIONDATE,FILE_WRITEDATE,");
		queryString.append(" FILE_INCORPORATEDATE,FILE_PUBLISHINGCOMPANY,FILE_SUBJECT,FILE_SUMMARY,FILE_REMARKS,FILE_AUDITOR,FILE_CHECKDATE,");
		queryString.append(" FILE_LOOKNEWDAYS,FILE_LOOKNEWDATE,FILE_CHECKSUGGESTION,FILE_ISPUBLIC,FILE_TYPE,FILE_ADD,SETUP_USER_NAME,SETUP_TIME");
		queryString.append(" from TDM_FILE_MANAGE where 1=1 and FILE_TYPE = "+ fileType +" ");
		if (filterMap != null && !filterMap.isEmpty()) {
			//if(null != filterMap.get("fileType") && !"".equals(filterMap.get("fileType"))){
			//	queryCountString.append("and FILE_TYPE = '" +  filterMap.get("fileType") + "' ");
			//	queryString.append("and FILE_TYPE = '" +  filterMap.get("fileType") + "' ");
			//}
			if(null != filterMap.get("fileName") && !"".equals(filterMap.get("fileName"))){
				queryCountString.append("and FILE_NAME like '%" + filterMap.get("fileName") + "%'  ");
				queryString.append("and FILE_NAME like '%" + filterMap.get("fileName") + "%'  ");
			}
			if(null != filterMap.get("fileSampleId") && !"".equals(filterMap.get("fileSampleId"))){
				queryCountString.append("and FILE_SAMPLEID like '%" + filterMap.get("fileSampleId") + "%' ");
				queryString.append("and FILE_SAMPLEID like '%" + filterMap.get("fileSampleId") + "%' ");
			}
			//if(null != filterMap.get("fileDocumentType") && !"".equals(filterMap.get("fileDocumentType"))){
			//	String str = filterMap.get("fileDocumentType").toString();
			//	System.out.println(str);
//				queryString.append("and str like '%" + filterMap.get("fileDocumentType") + "%' ");
			//}
		}
		return searchBySQL(queryCountString.toString(), queryString.toString(), filterMap);
	}
	
	public List<KnowledgeManage> getAllKMList(Map filterMap) throws DaoException {
		//获取记录总数 
		List<KnowledgeManage> list = null;
		try {
			StringBuilder sqlStr = new StringBuilder("from KnowledgeManage ");
//			sqlStr.append(KnowledgeManage);
			sqlStr.append(" as entity");
			sqlStr.append(" where 1=1");
//			if (StringUtils.isNotEmpty(wheresql)) {
//				sqlStr.append(" where ");
//				sqlStr.append(wheresql);
//			}
			if (filterMap != null && !filterMap.isEmpty()) {
				if(null != filterMap.get("id") && !"".equals(filterMap.get("id"))){
					sqlStr.append(" and id =" +  filterMap.get("id"));
				}
			}
//			sqlStr.append(" order by entity.");
//			sqlStr.append(sort);
//			sqlStr.append(" ");
//			sqlStr.append(dir);
//			System.out.println(sqlStr);
			Session session = this.getSession();
			Query query = session.createQuery(sqlStr.toString());
//			query.setFirstResult(start);
//			query.setMaxResults(limit);
			list = query.list();
		} catch (Exception e) {
			throw new DaoException("查询时出现异常，请确认操作是否正确",e);
		}
		return list;
	}

	public void saveOrUpdate(Object o) throws DaoException{
		if(o == null){
			throw new DaoException("保存时传入参数为空，保存失败");
		}
		try{
			getHibernateTemplate().saveOrUpdate(o);
		}catch(Exception e){
			throw new DaoException("保存失败，请确认操作是否正确",e);
		}
	}
	
	public PageResult queryUsersByRoleId(Map filterMap) {
		StringBuffer queryString = new StringBuffer();
		queryString.append("SELECT T.USERID as id, T.LOGINNAME,T.USERNAME,t.org_id, T1.ORG_NAME "+
						   "FROM USERS T LEFT JOIN ORGANIZATION T1 ON T.ORG_ID = T1.ORG_ID "+
						   "WHERE 1=1 ");
		StringBuffer countString = new StringBuffer();
		countString.append("SELECT count(*) FROM USERS T LEFT JOIN ORGANIZATION T1 ON T.ORG_ID = T1.ORG_ID WHERE 1=1");
		if (filterMap != null && !filterMap.isEmpty()) {
			String filterProperty;
			String filterValue;
		
			filterProperty = "userName";
			filterValue = (String) filterMap.get(filterProperty);
			if (!StringUtil.isMissing(filterValue)) {
				queryString.append(" and T.USERNAME ").append(" like'%").append(filterValue).append("%'");
				countString.append(" and T.USERNAME ").append(" like'%").append(filterValue).append("%'");
			}
			filterProperty = "deptName";
			filterValue = (String) filterMap.get(filterProperty);
			if (!StringUtil.isMissing(filterValue)) {
				queryString.append(" and T1.ORG_NAME ").append(" like'%").append(filterValue).append("%'");
				countString.append(" and T1.ORG_NAME ").append(" like'%").append(filterValue).append("%'");
			}
			String sidx = (String)filterMap.get("sidx");
			String sord = (String)filterMap.get("sord");
			queryString.append(" order by t." + sidx + " " + sord );
		}
		return searchBySQL(countString.toString(),queryString.toString(),filterMap);
	}

	public void delKnowledgeManage(Long id) {
		Session session = this.getSession();
		Query query = null;
		StringBuilder sb = new StringBuilder();
		sb.append(" delete ");
		sb.append(" KnowledgeManage");
		sb.append(" as entity");
		sb.append(" where entity.id = ");
		sb.append( id);
		query = session.createQuery(sb.toString());
		query.executeUpdate();
	}

	public KnowFileType getKnowledgeByName(String typeName) {
		List list=this.getHibernateTemplate().find("from KnowFileType k where k.typeName = '"+ StringUtil.escapeString(typeName)+"' ");
		if(list != null && list.size()>0){
			return (KnowFileType)list.get(0);
		}
		return null;
	}

	/**
	 * 判断文件类别下是否有文件
	 * @throws IOException
	 * @throws ServiceException void
	 * @author 张海娜
	 * @date 2012-10-19 下午06:30:38
	 */
	public PageResult getCountKnowledgeTypeByFileType(Long id,
			Map<String, String> filterMap) {
		StringBuffer queryString = new StringBuffer();
		StringBuffer queryCountString = new StringBuffer();
		queryString.append("select t.ID from tdm_file_manage t left join TDM_FILE_DOCUMENT_TYPE tf on t.file_type = tf.id where tf.id= "+id+" ");
		queryCountString.append("select count(*) from tdm_file_manage t left join TDM_FILE_DOCUMENT_TYPE tf on t.file_type = tf.id where tf.id= "+id+" ");
		return searchBySQL(queryCountString.toString(), queryString.toString(), filterMap);
	}

	/**
	 * 查询所有的标签
	 * @param filterMap
	 * @return PageResult
	 * @author 张海娜
	 * @date 2012-10-26 下午02:31:39
	 */
	public PageResult getAllKnowFileType(Map<String, String> filterMap) {
		StringBuffer queryString = new StringBuffer();
		StringBuffer queryCountString = new StringBuffer();
		queryString.append(" select t.id,t.type_name,t.type_desc,t.type_state from dict_file_type t ");
		queryCountString.append(" select count(*) from dict_file_type t  ");
		return searchBySQL(queryCountString.toString(), queryString.toString(), filterMap);
	}

	
	public PageResult getAllKnowledgeManageShouCangExec(
			Map<String, String> filterMap) {
		StringBuffer queryString = new StringBuffer();
		StringBuffer queryCountString = new StringBuffer();
		
		queryCountString.append("select count(*) from ( select m.ID,m.FILE_NAME,m.FILE_SAMPLEID,m.FILE_DOCUMENTTYPE,m.file_documenttype_name,m.FILE_ENTRYADDRESS,m.FILE_AUTHOR,m.FILE_VERSION, "+
						   "  m.FILE_PAGE,m.FILE_VOLUMENUMBER,m.FILE_CUSTOS,m.FILE_CLASSIFICATIONTYPE,m.FILE_PRICE,m.FILE_PUBLICATIONDATE,m.FILE_WRITEDATE, "+
						   "  m.FILE_INCORPORATEDATE,m.FILE_PUBLISHINGCOMPANY,m.FILE_SUBJECT,m.FILE_SUMMARY,m.FILE_REMARKS,m.FILE_AUDITOR,m.FILE_CHECKDATE, "+
						   "  m.FILE_LOOKNEWDAYS,m.FILE_LOOKNEWDATE,m.FILE_CHECKSUGGESTION,m.FILE_ISPUBLIC,m.FILE_TYPE,m.FILE_ADD,m.SETUP_USER_NAME,m.SETUP_TIME "+
						   " from TDM_FILE_MANAGE m left join  TDM_KNOWLEDGE_HANDOUT t on m.id= t.knowledgeid where 1=1 ");
		queryString.append("select m.ID,m.FILE_NAME,m.FILE_SAMPLEID,m.FILE_DOCUMENTTYPE,m.file_documenttype_name,m.FILE_ENTRYADDRESS,m.FILE_AUTHOR,m.FILE_VERSION, "+
						   "  m.FILE_PAGE,m.FILE_VOLUMENUMBER,m.FILE_CUSTOS,m.FILE_CLASSIFICATIONTYPE,m.FILE_PRICE,m.FILE_PUBLICATIONDATE,m.FILE_WRITEDATE, "+
						   "  m.FILE_INCORPORATEDATE,m.FILE_PUBLISHINGCOMPANY,m.FILE_SUBJECT,m.FILE_SUMMARY,m.FILE_REMARKS,m.FILE_AUDITOR,m.FILE_CHECKDATE, "+
						   "  m.FILE_LOOKNEWDAYS,m.FILE_LOOKNEWDATE,m.FILE_CHECKSUGGESTION,m.FILE_ISPUBLIC,m.FILE_TYPE,m.FILE_ADD,m.SETUP_USER_NAME,m.SETUP_TIME "+
						   " from TDM_FILE_MANAGE m left join  TDM_KNOWLEDGE_HANDOUT t on m.id= t.knowledgeid where 1=1 ");
		if (filterMap != null && !filterMap.isEmpty()) {
			if(null != filterMap.get("userid") && !"".equals(filterMap.get("userid"))){
				queryCountString.append("and t.userid = '" +  filterMap.get("userid") + "' ");
				queryString.append("and t.userid = '" +  filterMap.get("userid") + "' ");
			}
			if(null != filterMap.get("fileType") && !"".equals(filterMap.get("fileType"))){
				queryCountString.append("and m.FILE_TYPE = '" +  filterMap.get("fileType") + "' ");
				queryString.append("and m.FILE_TYPE = '" +  filterMap.get("fileType") + "' ");
			}
			if(null != filterMap.get("fileName") && !"".equals(filterMap.get("fileName"))){
				queryCountString.append("and m.FILE_NAME like '%" + filterMap.get("fileName") + "%'  ");
				queryString.append("and m.FILE_NAME like '%" + filterMap.get("fileName") + "%'  ");
			}
			if(null != filterMap.get("fileSampleId") && !"".equals(filterMap.get("fileSampleId"))){
				queryCountString.append("and m.FILE_SAMPLEID like '%" + filterMap.get("fileSampleId") + "%' ");
				queryString.append("and m.FILE_SAMPLEID like '%" + filterMap.get("fileSampleId") + "%' ");
			}
			if(null != filterMap.get("fileDocumentTypeName") && !"".equals(filterMap.get("fileDocumentTypeName"))){
				queryCountString.append("and m.file_documenttype_name like '%" + filterMap.get("fileDocumentTypeName") + "%' ");
				queryString.append("and m.file_documenttype_name like '%" + filterMap.get("fileDocumentTypeName") + "%' ");
			}
			String sidx = (String) filterMap.get("sidx");
			String sord = (String) filterMap.get("sord");
			queryString.append(" order by " + sidx + " " + sord + " ");
			queryCountString.append(") ");
		}
		return searchBySQL(queryCountString.toString(), queryString.toString(), filterMap);
	
	}

	
	public KnowledgeManage findFileAddressById(long id) {
		KnowledgeManage knowledgeManage = (KnowledgeManage)super.getHibernateTemplate().get(KnowledgeManage.class, id);
		return knowledgeManage;
	}

	
	public PageResult getAllKnowledgeManageAfterByUserIdExec(
			Map<String, String> filterMap) {
		StringBuffer queryString = new StringBuffer();
		StringBuffer queryCountString = new StringBuffer();
		
		queryCountString.append("select count(*) from (select m.ID,m.FILE_NAME,m.FILE_SAMPLEID,m.FILE_DOCUMENTTYPE,m.file_documenttype_name,m.FILE_ENTRYADDRESS,m.FILE_AUTHOR,m.FILE_VERSION, "+
						   "  m.FILE_PAGE,m.FILE_VOLUMENUMBER,m.FILE_CUSTOS,m.FILE_CLASSIFICATIONTYPE,m.FILE_PRICE,m.FILE_PUBLICATIONDATE,m.FILE_WRITEDATE, "+
						   "  m.FILE_INCORPORATEDATE,m.FILE_PUBLISHINGCOMPANY,m.FILE_SUBJECT,m.FILE_SUMMARY,m.FILE_REMARKS,m.FILE_AUDITOR,m.FILE_CHECKDATE, "+
						   "  m.FILE_LOOKNEWDAYS,m.FILE_LOOKNEWDATE,m.FILE_CHECKSUGGESTION,m.FILE_ISPUBLIC,m.FILE_TYPE,m.FILE_ADD,m.SETUP_USER_NAME,m.SETUP_TIME "+
						   " from TDM_FILE_MANAGE m left join  TDM_KNOWLEDGE_HANDOUT t on m.id= t.knowledgeid where 1=1   ");
		queryString.append("select m.ID,m.FILE_NAME,m.FILE_SAMPLEID,m.FILE_DOCUMENTTYPE,m.file_documenttype_name,m.FILE_ENTRYADDRESS,m.FILE_AUTHOR,m.FILE_VERSION, "+
						   "  m.FILE_PAGE,m.FILE_VOLUMENUMBER,m.FILE_CUSTOS,m.FILE_CLASSIFICATIONTYPE,m.FILE_PRICE,m.FILE_PUBLICATIONDATE,m.FILE_WRITEDATE, "+
						   "  m.FILE_INCORPORATEDATE,m.FILE_PUBLISHINGCOMPANY,m.FILE_SUBJECT,m.FILE_SUMMARY,m.FILE_REMARKS,m.FILE_AUDITOR,m.FILE_CHECKDATE, "+
						   "  m.FILE_LOOKNEWDAYS,m.FILE_LOOKNEWDATE,m.FILE_CHECKSUGGESTION,m.FILE_ISPUBLIC,m.FILE_TYPE,m.FILE_ADD,m.SETUP_USER_NAME,m.SETUP_TIME "+
						   " from TDM_FILE_MANAGE m left join  TDM_KNOWLEDGE_HANDOUT t on m.id= t.knowledgeid where 1=1 ");
		if (filterMap != null && !filterMap.isEmpty()) {
			if(null != filterMap.get("userid") && !"".equals(filterMap.get("userid"))){
				queryCountString.append("and t.userid = '" +  filterMap.get("userid") + "' ");
				queryString.append("and t.userid = '" +  filterMap.get("userid") + "' ");
			}
			if(null != filterMap.get("fileType") && !"".equals(filterMap.get("fileType"))){
				queryCountString.append("and m.FILE_TYPE = '" +  filterMap.get("fileType") + "' ");
				queryString.append("and m.FILE_TYPE = '" +  filterMap.get("fileType") + "' ");
			}
			if(null != filterMap.get("fileName") && !"".equals(filterMap.get("fileName"))){
				queryCountString.append("and m.FILE_NAME like '%" + filterMap.get("fileName") + "%'  ");
				queryString.append("and m.FILE_NAME like '%" + filterMap.get("fileName") + "%'  ");
			}
			if(null != filterMap.get("fileSampleId") && !"".equals(filterMap.get("fileSampleId"))){
				queryCountString.append("and m.FILE_SAMPLEID like '%" + filterMap.get("fileSampleId") + "%' ");
				queryString.append("and m.FILE_SAMPLEID like '%" + filterMap.get("fileSampleId") + "%' ");
			}
			if(null != filterMap.get("fileDocumentTypeName") && !"".equals(filterMap.get("fileDocumentTypeName"))){
				queryCountString.append("and m.file_documenttype_name like '%" + filterMap.get("fileDocumentTypeName") + "%' ");
				queryString.append("and m.file_documenttype_name like '%" + filterMap.get("fileDocumentTypeName") + "%' ");
			}
			queryCountString.append(")");
		}
		return searchBySQL(queryCountString.toString(), queryString.toString(), filterMap);
	}

	
	public PageResult getAllKnowledgeManageByPatentIdByUserIdExec(String fileType, 
			Map<String, String> filterMap) {
		StringBuffer queryString = new StringBuffer();
		StringBuffer queryCountString = new StringBuffer();
		
		queryCountString.append("select count(*) from (select m.ID,m.FILE_NAME,m.FILE_SAMPLEID,m.FILE_DOCUMENTTYPE,m.file_documenttype_name,m.FILE_ENTRYADDRESS,m.FILE_AUTHOR,m.FILE_VERSION, "+
						   "  m.FILE_PAGE,m.FILE_VOLUMENUMBER,m.FILE_CUSTOS,m.FILE_CLASSIFICATIONTYPE,m.FILE_PRICE,m.FILE_PUBLICATIONDATE,m.FILE_WRITEDATE, "+
						   "  m.FILE_INCORPORATEDATE,m.FILE_PUBLISHINGCOMPANY,m.FILE_SUBJECT,m.FILE_SUMMARY,m.FILE_REMARKS,m.FILE_AUDITOR,m.FILE_CHECKDATE, "+
						   "  m.FILE_LOOKNEWDAYS,m.FILE_LOOKNEWDATE,m.FILE_CHECKSUGGESTION,m.FILE_ISPUBLIC,m.FILE_TYPE,m.FILE_ADD,m.SETUP_USER_NAME,m.SETUP_TIME "+
						   " from TDM_FILE_MANAGE m left join  TDM_KNOWLEDGE_HANDOUT t on m.id= t.knowledgeid where 1=1  ");
		queryString.append("select m.ID,m.FILE_NAME,m.FILE_SAMPLEID,m.FILE_DOCUMENTTYPE,m.file_documenttype_name,m.FILE_ENTRYADDRESS,m.FILE_AUTHOR,m.FILE_VERSION, "+
						   "  m.FILE_PAGE,m.FILE_VOLUMENUMBER,m.FILE_CUSTOS,m.FILE_CLASSIFICATIONTYPE,m.FILE_PRICE,m.FILE_PUBLICATIONDATE,m.FILE_WRITEDATE, "+
						   "  m.FILE_INCORPORATEDATE,m.FILE_PUBLISHINGCOMPANY,m.FILE_SUBJECT,m.FILE_SUMMARY,m.FILE_REMARKS,m.FILE_AUDITOR,m.FILE_CHECKDATE, "+
						   "  m.FILE_LOOKNEWDAYS,m.FILE_LOOKNEWDATE,m.FILE_CHECKSUGGESTION,m.FILE_ISPUBLIC,m.FILE_TYPE,m.FILE_ADD,m.SETUP_USER_NAME,m.SETUP_TIME "+
						   " from TDM_FILE_MANAGE m left join  TDM_KNOWLEDGE_HANDOUT t on m.id= t.knowledgeid where 1=1 ");
		if (filterMap != null && !filterMap.isEmpty()) {
			if(null != filterMap.get("fileType") && !"".equals(filterMap.get("fileType"))){
				queryCountString.append("and FILE_TYPE = '" +  filterMap.get("fileType") + "' ");
				queryString.append("and FILE_TYPE = '" +  filterMap.get("fileType") + "' ");
			}
			if(null != filterMap.get("userid") && !"".equals(filterMap.get("userid"))){
				queryCountString.append("and t.userid = '" +  filterMap.get("userid") + "' ");
				queryString.append("and t.userid = '" +  filterMap.get("userid") + "' ");
			}
			if(null != filterMap.get("fileName") && !"".equals(filterMap.get("fileName"))){
				queryCountString.append("and m.FILE_NAME like '%" + filterMap.get("fileName") + "%'  ");
				queryString.append("and m.FILE_NAME like '%" + filterMap.get("fileName") + "%'  ");
			}
			if(null != filterMap.get("fileSampleId") && !"".equals(filterMap.get("fileSampleId"))){
				queryCountString.append("and m.FILE_SAMPLEID like '%" + filterMap.get("fileSampleId") + "%' ");
				queryString.append("and m.FILE_SAMPLEID like '%" + filterMap.get("fileSampleId") + "%' ");
			}
			//if(null != filterMap.get("fileDocumentType") && !"".equals(filterMap.get("fileDocumentType"))){
			//	String str = filterMap.get("fileDocumentType").toString();
			//	System.out.println(str);
//				queryString.append("and str like '%" + filterMap.get("fileDocumentType") + "%' ");
			//}
			queryCountString.append(")");
		}
		return searchBySQL(queryCountString.toString(), queryString.toString(), filterMap);
	}

	
	public String findFenFa(String uid, String id) {
		String sql = "select count(*) from TDM_KNOWLEDGE_HANDOUT t  where t.userid="+uid+" and t.knowledgeid="+id+" ";
		Query query = this.getSession().createSQLQuery(sql);
		List list = query.list();
		return String.valueOf(list.get(0));
	}

	
	public PageResult searchFenFaUserExec(Map<String, String> filterMap) {
		StringBuffer queryString = new StringBuffer();
		StringBuffer queryCountString = new StringBuffer();
		String userids = filterMap.get("userids");
		queryCountString.append("select count(*) from (select s.userid,s.username,o.org_name from SYS_USERS s left join SYS_ORGANIZATION o on s.org_id=o.org_id where 1=1   ");
		queryString.append("select s.userid,s.username,o.org_name from SYS_USERS s left join SYS_ORGANIZATION o on s.org_id=o.org_id where 1=1 ");
		if (filterMap != null && !filterMap.isEmpty()) {
			if(null != userids && !"".equals(userids)){
				queryCountString.append(" and s.userid not in ("+userids+") ");
				queryString.append(" and  s.userid not in ("+userids+")  ");
			}
			if(null != filterMap.get("username") && !"".equals(filterMap.get("username"))){
				queryCountString.append("and s.username like '%" + filterMap.get("username") + "%'  ");
				queryString.append("and s.username like '%" + filterMap.get("username") + "%'  ");
			}
			if(null != filterMap.get("orgId") && !"".equals(filterMap.get("orgId"))){
				queryCountString.append("and o.org_id = '" + filterMap.get("orgId") + "' ");
				queryString.append("and o.org_id = '" + filterMap.get("orgId") + "' ");
			}
			queryCountString.append(")");
		}
		return searchBySQL(queryCountString.toString(), queryString.toString(), filterMap);
	}

	
	public List<KnowledgeHandOut> findUserids(String knowledgeid) {
		List list=this.getHibernateTemplate().find("from KnowledgeHandOut k where k.knowledgeId= '"+ StringUtil.escapeString(knowledgeid)+"' ");
		return list;
	}

	
	public List<KnowledgeHandOut> findKnowledgeHanderOut(long id) {
		List list=this.getHibernateTemplate().find("from KnowledgeHandOut k where k.knowledgeId= '"+ id+"' ");
		return list;
	}

	
	public void delknowledgeHandOut(long id) {
		Session session = this.getSession();
		Query query = null;
		StringBuilder sb = new StringBuilder();
		sb.append(" delete ");
		sb.append(" KnowledgeHandOut");
		sb.append(" as entity");
		sb.append(" where entity.id = ");
		sb.append( id);
		query = session.createQuery(sb.toString());
		query.executeUpdate();
	}

	
	


}
